package com.etoak.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etoak.common.core.enums.OrderStateEnum;
import com.etoak.entity.Order;
import com.etoak.mapper.OrderMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CancelService {
    @Autowired
    OrderMapper orderMapper;
    @RabbitListener(queues = "order")
    public void cancel(String orderNo){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        Order order = orderMapper.selectOne(queryWrapper);

        if (ObjectUtils.isNotEmpty(order)&& order.getOrderState().equals(OrderStateEnum.NEW.getValue())){
            order.setOrderState(OrderStateEnum.CANCEL.getValue());
            order.setCancelTime(DateUtil.now());
            order.setCancelReason("超时未支付订单已经取消");
            orderMapper.updateById(order);
        }
    }
}
